package com.example.mbean;

import javax.management.*;
import java.lang.management.ManagementFactory;

/**
 * @author shanchong
 * @date 2019-07-29
 **/
public class MBeanDemo {

    /**
     * jconsole链接后操作MBean
     * @param args
     */
    public static void main(String[] args) {
        final User userMBean = new User();

        try {
            final ObjectName objectName = new ObjectName("com.example.mbean:type=User");
            final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
            mBeanServer.registerMBean(userMBean,objectName);
        } catch (InstanceAlreadyExistsException e) {
            e.printStackTrace();
        } catch (MBeanRegistrationException e) {
            e.printStackTrace();
        } catch (NotCompliantMBeanException e) {
            e.printStackTrace();
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        }

        while (true){
            System.out.println(userMBean.getName()+":"+userMBean.getPassword());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
